package com.sky.utils;


import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
@Slf4j
@Component
public class FileUtil {
    @Autowired
    private AliOssUtil aliOssUtil;
    //将图片转为网址直接访问的地址
    public String fileutil(MultipartFile file){


        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取后缀，判断是什么类型的图片
            originalFilename = originalFilename.substring(originalFilename.lastIndexOf("."));
            //使用uuid作为他的新名字
            String objectname= UUID.randomUUID().toString()+originalFilename;
            String object=aliOssUtil.upload(file.getBytes(),objectname);
           return object;
        } catch (IOException e) {
            log.error("文件上传失败：{}",e.getMessage());
            throw new RuntimeException(e);
        }


    }
}
